package com.yiaohuer.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.yiaohuer.DTO.UserDTO;
import com.yiaohuer.VO.UserVO;
import com.yiaohuer.dao.UserDao;
import com.yiaohuer.entity.Result;
import com.yiaohuer.entity.User;
import com.yiaohuer.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/campus/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody UserDTO userDTO){
        if (userDTO.getUserId() == null || userDTO.getPassword() == null){
            return Result.fail("请正确输入账号密码");
        }
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        UserVO userVO = userService.login(user);
        if (userVO.getUserId() == null){
            return Result.fail();
        }
        return Result.ok(userVO);
    }

    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("user",null);
        return Result.ok("退出成功");
    }
}
